<template>
  <div>
    <editForm @submit="submit"></editForm>
  </div>
</template>

<script>
import md5 from "js-md5";
import editForm from "@/views/mechanism/components/editForm";
import { addBusines } from "@/api/mechanism";
export default {
  name: "AddMechanism",
  components: {
    editForm,
  },
  data() {
    return {
      add: false,
    };
  },
  beforeRouteLeave(to, form, next) {
    if (!this.add) {
      this.$confirm("离开当前页面编辑内容将会被清空, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$store.dispatch("tagsView/delView", this.$route);
          next();
        })
        .catch(() => {
          next(false);
        });
    } else {
      next();
    }
  },
  computed: {},
  created() {},
  methods: {
    submit(data) {
      const {
        name,
        password,
        logoImg,
        principalName,
        serverPhone,
        principalPhone,
        synopsis,
        teachCount,
        province,
        city,
        region,
        town,
        detail,
        lat,
        lng,
        addressIds,
        synopsisImg,
        categoryParentIds,
        categoryParentNames,
        categoryIds,
        categoryNames,
        studentCount,
        foundTime,
        labels,
        addressRemarks,
        type,
        doBusinessTime,
      } = data;
      addBusines({
        score: 5,
        parentId: this.$store.getters.businessId,
        password: md5(password),
        name: name,
        logoImg: logoImg,
        principalName: principalName,
        serverPhone: serverPhone,
        synopsis: synopsis,
        teachCount: teachCount,
        province: province,
        city: city,
        region: region,
        town: town,
        detail: detail,
        lat: lat,
        lng: lng,
        addressIds: addressIds,
        synopsisImg: synopsisImg,
        categoryIds: categoryIds,
        categoryParentIds: categoryParentIds,
        categoryParentNames: categoryParentNames,
        categoryNames: categoryNames,
        studentCount: studentCount,
        foundTime: foundTime,
        labels: labels,
        addressRemarks: addressRemarks,
        type: type,
        doBusinessTime,
      }).then((res) => {
        this.$message({
          message: "新增成功",
          type: "success",
        });
        this.add = true;
        this.$store.dispatch("tagsView/delView", this.$route);
        this.$router.push("/mechanism/school/list");
      });
    },
  },
};
</script>
